package cn.iocoder.yudao.module.famous.doctor.controller.admin.prescription;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.famous.doctor.controller.admin.prescription.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.prescription.PrescriptionDO;
import cn.iocoder.yudao.module.famous.doctor.convert.prescription.PrescriptionConvert;
import cn.iocoder.yudao.module.famous.doctor.service.prescription.PrescriptionService;

@Tag(name = "管理后台 - 药方统计")
@RestController
@RequestMapping("/famous.doctor/prescription")
@Validated
public class PrescriptionController {

    @Resource
    private PrescriptionService prescriptionService;

    @PostMapping("/create")
    @Operation(summary = "创建药方统计")
    @PreAuthorize("@ss.hasPermission('famous.doctor:prescription:create')")
    public CommonResult<Long> createPrescription(@Valid @RequestBody PrescriptionCreateReqVO createReqVO) {
        return success(prescriptionService.createPrescription(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新药方统计")
    @PreAuthorize("@ss.hasPermission('famous.doctor:prescription:update')")
    public CommonResult<Boolean> updatePrescription(@Valid @RequestBody PrescriptionUpdateReqVO updateReqVO) {
        prescriptionService.updatePrescription(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除药方统计")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('famous.doctor:prescription:delete')")
    public CommonResult<Boolean> deletePrescription(@RequestParam("id") Long id) {
        prescriptionService.deletePrescription(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得药方统计")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('famous.doctor:prescription:query')")
    public CommonResult<PrescriptionRespVO> getPrescription(@RequestParam("id") Long id) {
        PrescriptionDO prescription = prescriptionService.getPrescription(id);
        return success(PrescriptionConvert.INSTANCE.convert(prescription));
    }

    @GetMapping("/list")
    @Operation(summary = "获得药方统计列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('famous.doctor:prescription:query')")
    public CommonResult<List<PrescriptionRespVO>> getPrescriptionList(@RequestParam("ids") Collection<Long> ids) {
        List<PrescriptionDO> list = prescriptionService.getPrescriptionList(ids);
        return success(PrescriptionConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得药方统计分页")
    @PreAuthorize("@ss.hasPermission('famous.doctor:prescription:query')")
    public CommonResult<PageResult<PrescriptionRespVO>> getPrescriptionPage(@Valid PrescriptionPageReqVO pageVO) {
        PageResult<PrescriptionDO> pageResult = prescriptionService.getPrescriptionPage(pageVO);
        return success(PrescriptionConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出药方统计 Excel")
    @PreAuthorize("@ss.hasPermission('famous.doctor:prescription:export')")
    @OperateLog(type = EXPORT)
    public void exportPrescriptionExcel(@Valid PrescriptionExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PrescriptionDO> list = prescriptionService.getPrescriptionList(exportReqVO);
        // 导出 Excel
        List<PrescriptionExcelVO> datas = PrescriptionConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "药方统计.xls", "数据", PrescriptionExcelVO.class, datas);
    }

}
